package com.maybach.core.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.maybach.common.model.PartnerDO;
import com.maybach.core.dal.mapper.ext.PartnerExtMapper;
import com.maybach.dubbo.api.BizResult;
import com.maybach.dubbo.api.model.Partner;
import com.maybach.dubbo.api.service.IPartnerService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("partnerService")
public class PartnerServiceImpl implements IPartnerService {

	@Autowired
	private PartnerExtMapper partnerMapper;

	@Override
	public BizResult<Map<Long, Partner>> queryPartners(List<Long> ids) {
		try {
			Map<Long, Partner> partnerMap = Maps.newHashMap();
			List<PartnerDO> partnerDOs = partnerMapper.selectByIds(ids);
			if (CollectionUtils.isNotEmpty(partnerDOs)) {
				for (PartnerDO partnerDO : partnerDOs) {
					Partner partner = new Partner();
					BeanUtils.copyProperties(partnerDO, partner);
					partnerMap.put(partnerDO.getId(), partner);
				}
				return BizResult.create(partnerMap);
			} else {
				log.error("Query partner fail.\nParam is " + JSON.toJSONString(ids));
				return BizResult.create("501", "Query partner fail.");
			}
		} catch (Exception e) {
			log.error("Exception happens.\nIds is " + JSON.toJSONString(ids), e);
			return BizResult.create("500", "Exception happens.");
		}
	}

}
